# -*- coding: utf-8 -*-
#   自定义LineEdit类
# @Time    ： 2023/2/16  16:59
# @Auther  ： 徐良
# File     :  lineEditParts.py
from PySide6.QtWidgets import QPlainTextEdit, QTextEdit, QLineEdit
from PySide6.QtGui import QColor, QTextFormat, QPainter, QDoubleValidator, QIntValidator, QValidator
from PySide6.QtCore import Qt, QRect, QSize
from share.baseFunction import quote, getListFormStrByVLine
from share.sqlFunction import getSWO
from mesParts.lineEdit.lineNumberArea import LineNumberArea
from PySide6.QtCore import QRegularExpression
# 设置样式表
css1 = ("""
    /* 设置文本框背景颜色 */
    QLineEdit {
        background-color: #f0f0f0;
        border: 2px solid #ccc;  /* 设置边框 */
        border-radius: 5px;  /* 设置边框圆角 */
        padding: 5px;  /* 设置内边距 */
    }

    /* 设置文本框激活状态下的边框颜色 */
    QLineEdit:focus {
        border-color: #0078d7;
    }

    /* 设置文本框内部的光标颜色 */
    QLineEdit::cursor {
        width: 2px;  /* 光标宽度 */
        background-color: #0078d7;  /* 光标颜色 */
    }

    /* 设置文本框中的文字颜色 */
    QLineEdit::placeholder {
        color: #999;  /* 文字颜色 */
    }
""")

css2 = ("""
    /* 设置文本框背景颜色 */
    QLineEdit {
        background-color: #f0f0f0;
        border: 2px solid #ccc;  /* 设置边框 */
        border-radius: 5px;  /* 设置边框圆角 */
        padding: 5px;  /* 设置内边距 */
    }

    /* 设置文本框激活状态下的边框颜色 */
    QLineEdit:focus {
        border-color: #0078d7;
    }

    /* 设置文本框内部的光标颜色 */
    QLineEdit::cursor {
        width: 2px;  /* 光标宽度 */
        background-color: #0078d7;  /* 光标颜色 */
    }

    /* 设置文本框中的文字颜色 */
    QLineEdit::placeholder {
        color: red;  /* 文字颜色 */
    }
""")


class LineEditBase(QLineEdit):
    """
    基本编辑框
    """
    def __init__(self, parent=None):
        super(LineEditBase, self).__init__(parent)
        # self.returnPressed.connect(self.onEnterReturn)
        self.ROB = 0


    def setROB(self, value):
        if self.ROB != value:
            self.ROB = value
            self.setStyleSheet(css1) if self.ROB == 1 else self.setStyleSheet(css2)

    # def focusNextChild(self, obj):
    #     o = self.getNextObj(obj)
    #     if o is not None:
    #         o.setFocus()

    # def onEnterReturn(self):
    #
    #     self.focusNextChild(self)

    def setHoderText(self, sStr):
        self.setPlaceholderText(sStr)


class LineEdit1(LineEditBase):
    """
    任意输入的编辑框
    """
    def __init__(self, parent=None):
        super(LineEdit1, self).__init__(parent)
        self.returnPressed.connect(self.onEnterReturn)

    def focusNextChild(self, obj):
        o = self.getNextObj(obj)
        if o is not None:
            o.setFocus()

    def onEnterReturn(self):
        self.focusNextChild(self)

    def setHoderText(self, sStr):
        self.setPlaceholderText(sStr)


class BodyLineEdit1(LineEditBase):
    """
    表体的编辑框，任意输入的编辑框
    """
    def __init__(self, parent=None):
        super(BodyLineEdit1, self).__init__(parent)


    def setHoderText(self, sStr):
        """
        设置提示
        :param sStr:
        """
        self.setPlaceholderText(sStr)


# ------------------        用于tableView的LineEdit


class BodyLineEditInt(LineEditBase):
    """
    输入整数型
    """
    def __init__(self, parent=None):
        super(BodyLineEditInt, self).__init__(parent)
        # 创建整数校验器
        validator = QIntValidator(self)
        # 将整数校验器应用于QLineEdit
        self.setValidator(validator)


class BodyLineEditFloat(LineEditBase):
    """
    输入浮点数
    """
    def __init__(self, decimals=2, parent=None):
        super().__init__(parent)

        # 创建浮点数校验器
        self.validator = QDoubleValidator(self)
        self.validator.setDecimals(decimals)  # 设置小数位数

        # 将浮点数校验器应用于QLineEdit
        self.setValidator(self.validator)

    def setDecimals(self, decimals):
        # 设置小数位数
        self.validator.setDecimals(decimals)


class BodyLineEditStr(LineEditBase):
    """
    输入任意字符
    """
    def __init__(self, parent=None):
        super().__init__(parent)


# *******************************************  表头控件     *************************
class DecimalValidator(QValidator):
    def __init__(self, decimal_places, parent=None):
        super().__init__(parent)
        self.decimal_places = decimal_places

    def validate(self, input_str, pos):
        # 使用正则表达式验证输入是否为数字，可以包含一个小数点，小数点后最多指定的位数
        regexp = QRegularExpression(r'^\d*\.?\d{0,%d}$' % self.decimal_places)
        if regexp.exactMatch(input_str):
            return QValidator.Acceptable, input_str, pos
        elif input_str == '' or input_str == '.':
            return QValidator.Intermediate, input_str, pos
        else:
            return QValidator.Invalid, input_str, pos

class HeadLineEditBase(QLineEdit):
    """
    表头的LineEdit控件基础类
    """
    def __init__(self, parent=None):
        super(HeadLineEditBase, self).__init__(parent)
        self.returnPressed.connect(self.on_return_pressed)
        # 应用样式
        self.setStyleSheet("""
                    QLineEdit {
                        border: 1px solid #ccc;  /* 设置边框 */
                        border-radius: 3px;  /* 设置圆角 */
                        padding: 1px;  /* 设置内边距 */
                        font-size: 12px;  /* 设置字体大小 */
                    }
                    QPushButton {
                        background-color: #007bff;  /* 设置背景颜色 */
                        border: none;  /* 移除边框 */
                        color: #fff;  /* 设置文字颜色 */
                        border-radius: 5px;  /* 设置圆角 */
                    }
                    QPushButton:hover {
                        background-color: #0056b3;  /* 设置鼠标悬停时的背景颜色 */
                    }
                    QPushButton:pressed {
                        background-color: #003366;  /* 设置按钮按下时的背景颜色 */
                    }
                """)

    def on_return_pressed(self):
        self.focusNextChild()


class HeadLineEditStr(HeadLineEditBase):
    """
    输入任意字符
    """
    def __init__(self, pdict=None, parent=None):
        super(HeadLineEditStr, self).__init__(parent)
        if pdict is not None:
            if 'PlaceHolder' in pdict:
                self.setPlaceholderText(pdict['PlaceHolder'])  # 设置占位符文本



class HeadLineEditInt(HeadLineEditBase):
    """
    输入任意字符
    """
    def __init__(self, parent=None):
        super(HeadLineEditInt, self).__init__(parent)

        # 创建整数校验器，范围在 -2147483648 到 2147483647 之间
        validator = QIntValidator(-2147483648, 2147483647, self)
        self.setValidator(validator)


class HeadLineEditFloat(HeadLineEditBase):
    """
    输入任意字符
    """
    def __init__(self, decimal_places=2, parent=None):
        super(HeadLineEditFloat, self).__init__(parent)

        # 创建自定义的小数校验器，并指定小数位数
        validator = DecimalValidator(decimal_places)
        self.setValidator(validator)

